package juhouse.biz.kc.action;
import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import juhouse.biz.kc.dao.FeeBillDao;
import juhouse.biz.kc.dao.DBDao;
import juhouse.biz.kc.form.FeeBillForm;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

public class FeeBillAction  extends DispatchAction{

	public ActionForward saveFeeBill(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
			
		Enumeration<?> paramNames = request.getParameterNames(); 


		System.out.println("====================================");
		while (paramNames.hasMoreElements()) { 
		String paramName = (String)paramNames.nextElement(); 

		String[] paramValues = request.getParameterValues(paramName); 
        for (int i=0;i<paramValues.length;i++)
        {	
        	 System.out.println(paramName+"="+paramValues[i]);	
        }
		
		}

		System.out.println("====================================");
		
		if(request.getSession().getAttribute("manageId")!=null)
		{	
			FeeBillForm argForm = (FeeBillForm)form ;
			FeeBillDao dao = new FeeBillDao();
			int key=0;
			key=dao.saveFeeBill(argForm);
			String curUrl=this.getOpenCur(request, response, key);
			System.out.println(curUrl);
			response.sendRedirect(curUrl);
		}
		return null;
	}
	
	
	
	public ActionForward deleteFeeBill(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
			
		if(request.getSession().getAttribute("manageId")!=null)
		{	
	    	DBDao dao = new DBDao();
			String KeyStr = request.getParameter("keyStr");
			System.out.println("KeyStr="+KeyStr);
	        dao.exeSQL("delete from TFeeBill where billid="+KeyStr);
	        dao.exeSQL("delete from TFeeBillEntry where billid="+KeyStr);
	        this.sendRedirect(request, response);
		}
		return null;
	}
	
	
	
	public ActionForward sendFeeBill(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
			
		if(request.getSession().getAttribute("manageId")!=null)
		{	
	    	DBDao dao = new DBDao();
			String KeyStr = request.getParameter("keyStr");
			System.out.println("KeyStr="+KeyStr);
	        dao.exeSQL("update TFeeBill set billStatus=3 where billid="+KeyStr);
	        this.sendRedirect(request, response);
		}
		return null;
	}
		
	
	public ActionForward unSendFeeBill(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
			
		if(request.getSession().getAttribute("manageId")!=null)
		{	
	    	DBDao dao = new DBDao();
			String KeyStr = request.getParameter("keyStr");
			System.out.println("KeyStr="+KeyStr);
	        dao.exeSQL("update TFeeBill set billStatus=2 where billid="+KeyStr);
	        this.sendRedirect(request, response);
		}
		return null;
	}
		    
	public ActionForward cwCheckFeeBill(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		DBDao dao=new DBDao();
    	String keyStr = request.getParameter("keyStr");
    	String username =(String) request.getSession().getAttribute("manageName");
    	dao.exeSQL("update TFeeBill set cwName='"+username+"',cwDate=getdate() where billId="+keyStr);
        
        {
        	request.setAttribute("validateResult", "true");
        }
		return mapping.findForward("validateResult");
	}




	private void extracted(HttpServletRequest request) {
		request.setAttribute("validateResult", "false");
	}

	public ActionForward cwUnCheckFeeBill(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		DBDao dao=new DBDao();
    	String keyStr = request.getParameter("keyStr");
    	dao.exeSQL("update TFeeBill set cwName=null,cwDate=null where billId="+keyStr);
        
        if(1==1)
        {
        	request.setAttribute("validateResult", "true");
        }else{
        	extracted(request);
        }
		return mapping.findForward("validateResult");
	}
	
	
	public ActionForward checkFeeBill(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {		
		DBDao dao=new DBDao();
    	String keyStr = request.getParameter("keyStr");
    	dao.exeSQL("update TFeeBill set billStatus=2 where billId="+keyStr);
        if(1==1)
        {
        	request.setAttribute("validateResult", "true");
        }else{
        	extracted(request);
        }
		return mapping.findForward("validateResult");
	}
	
	
	
	public ActionForward unCheckFeeBill(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		DBDao dao=new DBDao();
    	String keyStr = request.getParameter("keyStr");
    	dao.exeSQL("update TFeeBill set billStatus=1 where billId="+keyStr);
        if(1==1)
        {
        	request.setAttribute("validateResult", "true");
        }else{
        	extracted(request);
        }
		return mapping.findForward("validateResult");
	}
	
	
	public ActionForward closeFeeBill(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {		
		DBDao dao=new DBDao();
    	String keyStr = request.getParameter("keyStr");
    	dao.exeSQL("update TFeeBill set billStatus=4 where billId="+keyStr);
        if(1==1)
        {
        	request.setAttribute("validateResult", "true");
        }else{
        	extracted(request);
        }
		return mapping.findForward("validateResult");
	}
	
	public ActionForward unCloseFeeBill(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		DBDao dao=new DBDao();
    	String keyStr = request.getParameter("keyStr");
    	dao.exeSQL("update TFeeBill set billStatus=3 where billId="+keyStr);
        if(1==1)
        {
        	request.setAttribute("validateResult", "true");
        }else{
        	extracted(request);
        }
		return mapping.findForward("validateResult");
	}
		
	
	public ActionForward shCheckFeeBill(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		DBDao dao=new DBDao();
    	String keyStr = request.getParameter("keyStr");
    	String username =(String) request.getSession().getAttribute("manageName");
    	dao.exeSQL("update TFeeBill set shName='"+username+"',shDate=getdate() where billId="+keyStr);
        
        if(1==1)
        {
        	request.setAttribute("validateResult", "true");
        }else{
        	extracted(request);
        }
		return mapping.findForward("validateResult");
	}
	
	public ActionForward shUnCheckFeeBill(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		DBDao dao=new DBDao();
    	String keyStr = request.getParameter("keyStr");
    	dao.exeSQL("update TFeeBill set shName=null,shDate=null where billId="+keyStr);
        
        if(1==1)
        {
        	request.setAttribute("validateResult", "true");
        }else{
        	extracted(request);
        }
		return mapping.findForward("validateResult");
	}
	
	public String getOpenCur(HttpServletRequest request,HttpServletResponse response,int key)throws IOException{
		StringBuffer sbufCurUrl = new StringBuffer();
        sbufCurUrl.append("http://");
        sbufCurUrl.append(request.getServerName());
        sbufCurUrl.append(":"+request.getServerPort());
        sbufCurUrl.append(request.getContextPath());
        sbufCurUrl.append("/mdpage/FeeBillEdit.jsp");
        String curUrl = sbufCurUrl.toString();
    	String url = request.getParameter("url");
        curUrl=curUrl+"?url="+url;
    	String pageno=null;
        pageno=request.getParameter("pageno");
        curUrl=curUrl+"&pageno="+pageno;
        String nowLineNum = request.getParameter("line");
        if (nowLineNum==null) nowLineNum="0";
        curUrl=curUrl+"&line="+nowLineNum;   
        if (key!=0)
        	curUrl=curUrl+"&keyStr="+key;
        return curUrl;
	}
	
    public void sendRedirect(HttpServletRequest request,HttpServletResponse response) throws IOException{
    	
    	String url = request.getParameter("url");
        String pageno=null;
        pageno=request.getParameter("pageno");
        	url=url+"?pageno="+pageno;
        String nowLineNum = request.getParameter("line");
        if (nowLineNum==null) nowLineNum="0";
        	url=url+"&line="+nowLineNum;
	    response.sendRedirect(url);	    
    }
}
